#include "SettingsWidget.h"
#include <SEUi/ImGui/imgui.h>
#include <SECore/Manager.h>
#include <SERender/Resourses/Texture.h>
#include <SECore/CoreApplication.h>
#include <SEEdit/Descriptors/RenderSettingDescriptor.h>
#include <SEUi/Layout/ColumnsLayout.h>

namespace SEngine
{
    SettingsWidget::SettingsWidget(EditLayer & layer)
        : Dialog()
        , m_layer(layer)
    {
        this->SetType(DialogType::Modal);
    }

    void SettingsWidget::OnUpdateCamera(Camera &camera)
    {
        SignalUpdateGUI.AddSlot([&camera]() {
            if (ImGui::BeginTabItem("Camera")) {
                ImGui::ColorEdit3("Clear Color", &camera.GetClearColor().x);
                ImGui::DragFloat("zNear", &camera.GetZNear(), 0.01f, 0.01f, camera.GetZFar());
                ImGui::DragFloat("zFar", &camera.GetZFar(), 0.01f, camera.GetZNear(), 1000.0f);
                ImGui::EndTabItem();
            }
            return SlotState::Once;
        });
    }

    void SettingsWidget::BeginUpdate()
    {
        ImGui::SetNextWindowSizeConstraints(
            ImVec2(600, 300),  // 最小宽高
            ImVec2(FLT_MAX, FLT_MAX) // 最大宽高（设为最大值表示不限制）
        );
    }

    void SettingsWidget::UpdateImpl(int delta)
    {
        auto & renderSettingDes = CoreApplication::App().GetDescriptor<RenderSettingDescriptor>()->get();

        ImGui::PushID(m_name.c_str());
        if (ImGui::BeginTabBar(("##" + m_name).c_str(), ImGuiTabBarFlags_None)) {
            if (ImGui::BeginTabItem("Render")) {
                ColumnsLayout<2> layout;
                layout.NewLine().NextColumn([&]() {
                    ImGui::Text("Grid"); 
                }).NextColumn([&]() {
                    ImGui::SliderFloat("##Grid", &renderSettingDes.grid, 0.0f, 1.0f);
                    ImGui::SameLine();
                    ImGui::Checkbox("##isShowGrid", &renderSettingDes.isShowGrid);
                });

                ImGui::EndTabItem();
            }
            if (ImGui::BeginTabItem("Light")) {
                ImGui::EndTabItem();
            }
            SignalUpdateGUI.Invoke();
            ImGui::EndTabBar();
        }
        ImGui::PopID();
    }
}